package util;

import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Random;

public class Util {
	static Random rnd = null;
	
	public static Random getRnd()
	{
		if(rnd == null) rnd = new Random();
		return rnd;
	}
	
	public static int getRndInt()
	{
		return getRnd().nextInt();
	}
	
	public static int getRndInt(int min, int max)
	{
		int r = Math.abs(getRnd().nextInt() % (max - min + 1)) + min;
		//System.out.println("getRndInt(" + min + ", " + max + ") = " + r);
		return r;
	}
	
	public static int getRndInt(int max)
	{
		int r = Math.abs(getRnd().nextInt() % max);
		//System.out.println("getRndInt(" + max + ") = " + r);
		return r;
	}
	
	public static boolean chance(int percent)
	{
		if(percent <= 0) return false;
		if(percent >= 100) return true;
		return getRndInt(100) <= percent;
	}
	
	public static float getDistF(float x1, float y1, float x2, float y2)
	{
		return (float) Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
	}
	
	public static String toUTF8(String str)
	{
		byte rawBytes[] = str.getBytes();
		System.out.println("Before : " + str);
		String s = new String(rawBytes, Charset.forName("UTF-8"));
		System.out.println(" After : " + s);
		return s;
	}
	
	public static String fixPath(String path)
	{
		String np = "";
		try
		{
			for(char c : path.toCharArray())
			{
				if(c == '\\' || c == '/')
				{
					np += File.separator;
				}
				else
				{
					np += c;
				}
			}
			return np;
		}
		catch(Exception e)
		{
			return "";
		}
	}
	
	private static Rectangle screen = null;
	public static Rectangle getScreenSize()
	{
		if(screen == null)
		{
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			GraphicsDevice[] gs = ge.getScreenDevices();

			for (int i=0; i<gs.length && i < 1; i++) {
			    DisplayMode dm = gs[i].getDisplayMode();
			    int screenWidth = dm.getWidth();
			    int screenHeight = dm.getHeight();
			    screen = new Rectangle(0, 0, screenWidth, screenHeight);
			}
		}
		return screen;
	}
}
